{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "US Population Over Time\n",
    "=======================\n",
    "This chart visualizes the age distribution of the US population over time.\n",
    "It uses a slider widget that is bound to the year to visualize the age\n",
    "distribution over time.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: interactive\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from altair.expr import datum, if_\n",
    "from vega_datasets import data\n",
    "\n",
    "pop = data.population.url\n",
    "\n",
    "pink_blue = alt.Scale(domain=('Male', 'Female'),\n",
    "                      range=[\"steelblue\", \"salmon\"])\n",
    "\n",
    "slider = alt.binding_range(min=1900, max=2000, step=10)\n",
    "year = alt.selection_single(name=\"year\", fields=['year'], bind=slider)\n",
    "\n",
    "alt.Chart(pop).mark_bar().encode(\n",
    "    x=alt.X('sex:N', axis=alt.Axis(title=None)),\n",
    "    y=alt.Y('people:Q', scale=alt.Scale(domain=(0, 12000000))),\n",
    "    color=alt.Color('sex:N', scale=pink_blue),\n",
    "    column='age:O'\n",
    ").properties(\n",
    "    width=20,\n",
    "    selection=year,\n",
    ").transform_calculate(\n",
    "    \"sex\", if_(datum.sex == 1, \"Male\", \"Female\")\n",
    ").transform_filter(\n",
    "    year.ref()\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
